package com.zyd.shiro.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallDictionaryDao;
import com.zyd.shiro.persistence.beans.Dictionary;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallDictionaryService {

	@Autowired
	private MallDictionaryDao dictionaryDao;
	
	

	public List<Dictionary> findAll() {

		return dictionaryDao.findAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Dictionary> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return dictionaryDao.findAll(pageRequest);
	}

	private Specification<Dictionary> where(Map searchMap) {
		
		return new Specification<Dictionary>() {
          
			@Override
			public Predicate toPredicate(Root<Dictionary> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                	predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 
                if (searchMap.get("value")!=null && !"".equals(searchMap.get("value"))) {
                	predicateList.add(cb.like(root.get("value").as(String.class), "%"+(String)searchMap.get("value")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<Dictionary> findSearch(Map whereMap, int page, int size) {
		Specification<Dictionary> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return dictionaryDao.findAll(specification, pageRequest);
	}

	public Dictionary findOne(Integer id) {
		return dictionaryDao.findOne(id);
	}

	public void add(Dictionary dictionary) {
		
		dictionaryDao.save(dictionary);
	}
	
	public void update(Dictionary dictionary) {
		dictionaryDao.save(dictionary);
	}

	public void delete(Integer id) {
		dictionaryDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			dictionaryDao.delete(id);
		}
	}

}
